package com.team.teamproject.ui.homepage;

import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.team.teamproject.R;
import com.team.teamproject.adapter.MyPagerFragmentAdapter;
import com.team.teamproject.bean.Channels;
import com.team.teamproject.utils.HttpUtils;
import com.team.teamproject.utils.ParseJson;

import java.util.ArrayList;
import java.util.List;

import static com.team.teamproject.R.id.tabLayout;


public class FragmentFirstPage extends Fragment {
    //-------首页--------

    private Toolbar mToolbar;
    private TabLayout mTabLayout;
    private Button mBtn_serach;
    private ImageView mIv_clock;
    private ViewPager mViewPager;
    //
    //网址
    String path="http://api.liwushuo.com/v2/channels/preset?gender=1&generation=1";
    //对话框
    private ProgressDialog dialog;
    private List<Channels> tl_list=new ArrayList<>();
    //标题标签
    List<String> titles = new ArrayList<>();
    private List<Fragment> fragments=new ArrayList<>();
    //适配器
    private MyPagerFragmentAdapter adapter;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 1:
                    dialog.dismiss();
                    Toast.makeText(getActivity(), "请检查你的网络...", Toast.LENGTH_SHORT).show();
                    break;
                case 2:
                    dialog.dismiss();
                    tl_list= (List<Channels>) msg.obj;
                    //遍历集合得到每一个name
                    for (Channels s:tl_list){
                        titles.add(s.getName());
                        //导航标签，根据需要设置显示的标题添加到tab中
                        // Log.i("Tag","标签每一项对应的ID-->"+s.getId());
                    }
                    for (int i = 0;i<titles.size();i++){
                        Log.i("Tag","title.size-->"+titles.size());
                        Bundle bundle=new Bundle();
                        //把tablayout获取的ID传给相应的ViewPager
                        //找到每一个ID
                        int id = tl_list.get(i).getId();
                        bundle.putInt("id",id);
                        if(id==100){
                            Log.i("aaa","要传给精选id"+id);
                            PagerFragment_JingXuan jingXuan=new PagerFragment_JingXuan();
                            jingXuan.setArguments(bundle);
                            fragments.add(jingXuan);
                        }else{
                            Log.i("aaa","要传给其他id"+id);
                            PagerFragment_Qita qita = new PagerFragment_Qita();
                            qita.setArguments(bundle);
                            fragments.add(qita);
                        }
                        adapter=new MyPagerFragmentAdapter(getFragmentManager(),titles,fragments);
                        mViewPager.setAdapter(adapter);
                    }
                    //TabLayout与ViewPager关联
                    mTabLayout.setupWithViewPager(mViewPager,true);
                    break;
                case 3:
                    dialog = new ProgressDialog(getActivity());
                    //dialog.setTitle("提示");
                    dialog.setMessage("正在加载中...");
                    dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                    dialog.show();
                    break;
            }
        }
    };
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_firstpage,null);
        //初始化控件
        mToolbar= (Toolbar) view.findViewById(R.id.toolBar);
        mTabLayout= (TabLayout) view.findViewById(tabLayout);
        mBtn_serach= (Button) view.findViewById(R.id.btn_firstpage_search);
        mIv_clock = (ImageView) view.findViewById(R.id.clock);
        mViewPager= (ViewPager) view.findViewById(R.id.firstpage_vp);
        //获取数据
        getData();
        //搜索
        search();
        //导航模式
        mTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        //设置导航标签的颜色
        mTabLayout.setTabTextColors(Color.BLACK, Color.RED);

        return view;
    }

    private void search() {
        mBtn_serach.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(),SearchActivity.class));
            }
        });
    }

    private void getData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                handler.sendEmptyMessage(3);
                String json = HttpUtils.getHttpUtils().getJson(path);
                if (json!=null){
                    List<Channels> channelses = ParseJson.fromTabLayoutTitle(json);
                    Message message = Message.obtain();
                    message.what = 2;
                    message.obj = channelses;
                    handler.sendMessage(message);
                }else{
                    handler.sendEmptyMessage(2);
                }
            }
        }).start();
    }
}
